package bdz.projects.lavender.opsm.utils {
	
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	
	/**
	 * ...
	 * @author ...
	 */
	public class EIHandler extends EventDispatcher {
		private var jsId:String;
		private static var instance:EIHandler;
		function EIHandler(se:SingletonEnforcer) {
			initialize();
		}
		public static function getInstance():EIHandler {
			if (instance == null) {
				instance = new EIHandler(new SingletonEnforcer());
				instance.initialize();
			}
			return instance;
		}
		protected function initialize():void {
			if(ExternalInterface.available){
			//create all callable functions by JS
				ExternalInterface.call("prepareFlashJS");
				ExternalInterface.addCallback("initializeJS", initJS);
			}
		}
		
		public function createCallback(alias:String, func:Function) {
			if(ExternalInterface.available){
				ExternalInterface.addCallback(alias, func);
			}
		}
		public function initJS(id:String):void {
			jsId = id;
		}
		public function notifyJavascript(func:String, ...args):void {
			if(ExternalInterface.available){
				ExternalInterface.call(jsId + ".notify", func, args);
			}
		}
		
		
		
	}
	
}
internal class SingletonEnforcer {}